package MyBlog.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import MyBlog.biz.AlbumBiz;
import MyBlog.biz.PhotoBiz;
import MyBlog.biz.UserBiz;
import MyBlog.entity.Album;
import MyBlog.entity.Photo;
import MyBlog.entity.User;

import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Component("photoAction")
public class PhotoAction extends ActionSupport {

	public String getMsg() {
		return msg;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public void setCover(File cover) {
		this.cover = cover;
	}

	public void setCoverFileName(String coverFileName) {
		this.coverFileName = coverFileName;
	}

	public List<Photo> getPhotoList() {
		return photoList;
	}

	public int getAlbumId() {
		return albumId;
	}

	public void setPhotoId(int photoId) {
		this.photoId = photoId;
	}

	public Photo getPhoto() {
		return photo;
	}

	public User getUser() {
		return user;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbumId(int albumId) {
		this.albumId = albumId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	// 参数
	private int userId;
	private int albumId;
	private int photoId;
	private Photo photo;
	private Album album;
	private User user;
	private String msg;

	// 数组
	private List<Album> albumList;
	private List<Photo> photoList;

	// 实现类
	@Autowired
	private PhotoBiz photoBiz;
	@Autowired
	private AlbumBiz albumBiz;
	@Autowired
	private UserBiz userBiz;

	// 方法
	public String list() {
		user = userBiz.fetchUserById(userId);
		photo = photoBiz.getPhotoById(photoId);
		return SUCCESS;
	}

	public String photoManage() {
		msg = (String) ServletActionContext.getRequest().getSession()
				.getAttribute("msg");
		user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("user");
		photoList = photoBiz.getPhotosByUserIdAndAlbumId(albumId, user.getId());
		ServletActionContext.getRequest().getSession()
		.setAttribute("msg",null);
		return SUCCESS;
	}

	private File cover;
	private String coverFileName;

	public String add() throws IOException{
		HttpSession sess = ServletActionContext.getRequest().getSession();
		sess.setAttribute("msg", null);
		Album a = albumBiz.getAlbumById(albumId);
		user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		if(cover == null ){
			sess.setAttribute("msg", "请选择图片路径");
			return ERROR;
		}
		photo.setUser(user);
		photo.setAlbum(a);
		photoBiz.add(photo);
		String exc = coverFileName.substring(coverFileName.lastIndexOf("."));
		String vpath = photo.getId()+exc;
		photo.setFilePath(vpath);
		vpath = "UserFiles/Albums/"+user.getId()+"/"+albumId+"/Thumbnail"+"/"+vpath;
		String realPath = ServletActionContext.getServletContext().getRealPath(vpath);
		System.out.println(realPath);
		File file = new File(realPath);
		FileUtils.copyFile(cover, file);
		realPath = realPath = "C:/程序设计/作业/it_homework/MyBlog/src/main/webapp"+
		"/UserFiles/Albums/"+photo.getUser().getId()+"/"+photo.getAlbum().getId()+"/Thumbnail/"+ photo.getFilePath();
		File descFile = new File(realPath);
		photoBiz.update(photo);
		return SUCCESS;
	}

}
